using System;

namespace GenericsFramework.BusinessRules
{
	public static class RulesExtensions
	{
		public static IRule<T> Check<T>(this Func<T, bool> when, Func<T, bool> rule)
		{
			return new BasicRule<T>(candidate => when(candidate) ? rule(candidate) : false);
		}

		public static Func<T, bool> And<T>(this IRule<T> rule)
		{
			return rule.IsSatisfiedBy;
		}

		public static IRule<T> And<T>(this IRule<T> ruleA, IRule<T> ruleB)
		{
			return new AndRule<T>(ruleA.IsSatisfiedBy, ruleB.IsSatisfiedBy);
		}

		public static IRule<T> Or<T>(this IRule<T> ruleA, IRule<T> ruleB)
		{
			return new OrRule<T>(ruleA.IsSatisfiedBy, ruleB.IsSatisfiedBy);
		}
	}
}